//
//  KAQueryDataBaseModel.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/25.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAQueryDataBaseModel.h"
#import "KADataBaseModel.h"

@implementation KAQueryDataBaseModel

+(KAQueryDataBaseModel *)instance
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
//查询当前日期的流水账
-(NSMutableArray *)queryCurrentDayData:(NSString *)currentDay
{
    NSMutableArray *currentDayMarr = [NSMutableArray array];
    NSArray *incomeArr = [[KADataBase instance] queryWithDate:currentDay otherWhere:@"收入"];
    NSMutableArray *marr1 = [NSMutableArray array];
    for (NSDictionary *dict in incomeArr) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [marr1 addObject:dbModel];
    }
    NSArray *outlayArr = [[KADataBase instance] queryWithDate:currentDay otherWhere:@"支出"];
    NSMutableArray *marr2 = [NSMutableArray array];
    for (NSDictionary *dict in outlayArr) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [marr2 addObject:dbModel];
    }
    [currentDayMarr addObject:marr1];
    [currentDayMarr addObject:marr2];
    
    return currentDayMarr;
}
//查询当前本月的流水账
-(NSMutableArray *)queryCurrentMonthData:(NSString *)currentMonth
{
    NSMutableArray *currentMonthMarr = [NSMutableArray array];
    NSMutableArray *marr1 = [NSMutableArray array];
    NSArray *incomeArr = [[KADataBase instance] queryWithBilltype:@"收入"];
    NSMutableArray *marrIncome = [NSMutableArray array];
    for (NSDictionary *dict in incomeArr) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [marrIncome addObject:dbModel];
    }
    for (KADataBaseModel *dbModel in marrIncome) {
        if ([dbModel.date hasPrefix:currentMonth]) {
            [marr1 addObject:dbModel];
        }
    }
    
    
    //支出
    NSMutableArray *marr2 = [NSMutableArray array];
    NSArray *outlayArr = [[KADataBase instance] queryWithBilltype:@"支出"];
    NSMutableArray *marrOutlay = [NSMutableArray array];
    for (NSDictionary *dict in outlayArr) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [marrOutlay addObject:dbModel];
    }
    for (KADataBaseModel *dbModel in marrOutlay) {
        if ([dbModel.date hasPrefix:currentMonth]) {
            [marr2 addObject:dbModel];
        }
    }
    [currentMonthMarr addObject:marr1];
    [currentMonthMarr addObject:marr2];
    
    return currentMonthMarr;
}
//查询当前本年的流水账
-(NSMutableArray *)queryCurrentYearData:(NSString *)currentYear
{
    NSMutableArray *currentYearMarr = [NSMutableArray array];

    NSMutableArray *marr1 = [NSMutableArray array];
    NSArray *incomeArr = [[KADataBase instance] queryWithBilltype:@"收入"];
    NSMutableArray *marrIncome = [NSMutableArray array];
    for (NSDictionary *dict in incomeArr) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [marrIncome addObject:dbModel];
    }
    for (KADataBaseModel *dbModel in marrIncome) {
        if ([dbModel.date hasPrefix:currentYear]) {
            [marr1 addObject:dbModel];
        }
    }
    
    //支出
    NSMutableArray *marr2 = [NSMutableArray array];
    NSArray *outlayArr = [[KADataBase instance] queryWithBilltype:@"支出"];
    NSMutableArray *marrOutlay = [NSMutableArray array];
    for (NSDictionary *dict in outlayArr) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [marrOutlay addObject:dbModel];
    }
    for (KADataBaseModel *dbModel in marrOutlay) {
        if ([dbModel.date hasPrefix:currentYear]) {
            [marr2 addObject:dbModel];
        }
    }
    
    [currentYearMarr addObject:marr1];
    [currentYearMarr addObject:marr2];
    
    return currentYearMarr;
}

@end
